package tornadofx.testapps

import javafx.beans.property.ReadOnlyListProperty
import javafx.beans.property.SimpleStringProperty
import javafx.collections.FXCollections
import javafx.geometry.Insets
import javafx.geometry.Orientation
import javafx.geometry.Pos
import javafx.scene.Parent
import javafx.scene.control.SelectionMode
import javafx.scene.layout.Priority
import javafx.scene.text.TextAlignment
import jfxtras.styles.jmetro.JMetro
import jfxtras.styles.jmetro.Style
import tornadofx.*
import tornadofx.testapps.DataGridTestApp.Companion.images

class DataGridTestApp : App(DataGridTest::class, DataGridStyles::class) {
    companion object {
        val images = mapOf(
                "kittens" to listOf(
                        "http://i.imgur.com/DuFZ6PQb.jpg",
                        "http://i.imgur.com/o2QoeNnb.jpg",
                        "http://i.imgur.com/P8wpBvub.jpg",
                        "http://i.imgur.com/a2NDDglb.jpg",
                        "http://i.imgur.com/79C37Scb.jpg",
                        "http://i.imgur.com/N0fPCU2b.jpg",
                        "http://i.imgur.com/qRfWC9wb.jpg",
                        "http://i.imgur.com/i3crLYcb.jpg"),

                "puppies" to listOf(
                        "http://i.imgur.com/37hCL2Pb.jpg",
                        "http://i.imgur.com/v9vBE67b.jpg",
                        "http://i.imgur.com/koQoEExb.jpg",
                        "http://i.imgur.com/qRfWC9wb.jpg",
                        "http://i.imgur.com/ftDV8oJb.jpg")

        )
    }

    override fun init() {
        super.init()
    }
}

class Item(name: String, icon: String) {
    var name by property(name)
    fun nameProperty() = getProperty(Item::name)
    var icon by property(icon)
    fun iconProperty() = getProperty(Item::icon)
}

class DataGridTest : View("DataGrid") {
    var datagrid: DataGrid<Item> by singleAssign()
    val list = FXCollections.observableArrayList<Item>()
    var paginator = DataGridPaginator(list, itemsPerPage = 55)

    init {

        list.add(Item("这是一张图片，名字超长，名字超长 超长超长 超长，超级超级长这是一张图片，名字超长，名字超长 超长超长 超长，超级超级长这是一张图片，名字超长，名字超长 超长超长 超长，超级超级长", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629497&di=86bff311893efdcd3a2335c92387dc61&imgtype=0&src=http%3A%2F%2Fa2.att.hudong.com%2F86%2F10%2F01300000184180121920108394217.jpg"))
        list.add(Item("这是一张图片，名字超长，名字超长 超长超长 超长，超级超级长这是一张图片，名字超长，名字超长 超长超长 超长，超级超级长这是一张图片，名字超长，名字超长 超长超长 超长，超级超级长", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=7cf51e43b0fdb4f5bf21d674c40fec6c&imgtype=0&src=http%3A%2F%2Fa1.att.hudong.com%2F05%2F00%2F01300000194285122188000535877.jpg"))
        list.add(Item("name21", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=c9e81c26009705cc24d11aecbc2f6a77&imgtype=0&src=http%3A%2F%2Fa3.att.hudong.com%2F14%2F75%2F01300000164186121366756803686.jpg"))
        list.add(Item("name22", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=cf4a79a4c80d260245944591e747c7a9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F27%2F67%2F01300000921826141299672233506.jpg"))
        list.add(Item("name23", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
        list.add(Item("name24", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
        list.add(Item("name25", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
        list.add(Item("name26", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
        list.add(Item("name27", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
        list.add(Item("name28", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
        list.add(Item("name29", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
        list.add(Item("name30", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1600103629498&di=0fdc047d2a96fcb049a2a095b52b9af9&imgtype=0&src=http%3A%2F%2Fa4.att.hudong.com%2F22%2F59%2F19300001325156131228593878903.jpg"))
    }

    override fun onBeforeShow() {
        super.onBeforeShow()
        JMetro(Style.LIGHT).scene = currentStage?.scene
        importStylesheet("/my.css")
    }

    private val tblList = FXCollections.observableArrayList<Item>(Item("Name", "icon"), Item("Name2", "Icon2"))

    override val root = borderpane {
        center {
            datagrid = datagrid(paginator.items) {
                setPrefSize(550.0, 550.0)

                selectionModel.selectionMode = SelectionMode.MULTIPLE
                cellWidth = 164.0
                cellHeight = 164.0

                cellCache {
                    vbox {
                        alignment = Pos.TOP_CENTER
                        flowpane() {
                            style = "-fx-border-width:1px; -fx-border-color:red;"
                            alignment = Pos.BOTTOM_CENTER
                            prefHeight = 96.0
                            maxHeight = 96.0
                            vboxConstraints { vGrow = Priority.ALWAYS }
                            stackpane() {
                                imageview(it.icon, true) {
                                    fitWidth = 96.0
                                    fitHeight = 96.0
                                    isPreserveRatio = true
                                }
                            }
                        }
                        label {
                            textAlignment = TextAlignment.CENTER
                            maxWidth = 96.0
                            prefWidth = 96.0
                            maxHeight = 48.0
                            isWrapText = true
                            text = it.name
                        }
                    }
                }

                onUserSelect(1) {
                    println("Selected $it")
                }
            }
        }
        bottom {
            scrollpane {
                prefHeight = 400.0
                flowpane {
                    paddingHorizontal = 10.0
                    paddingVertical = 10.0

                    button("按钮")
                    checkbox("复选框")
                    radiobutton("单选按钮")
                    combobox(values = arrayListOf("第一项", "第二项", "第三项", "第四项"))
                    progressbar()
                    progressbar(initialValue = 0.5)
                    slider()
                    slider(orientation = Orientation.VERTICAL)
                    textfield()
                    passwordfield()

                }
            }
        }
        left {
            tableview(items = tblList) {
                column("Name", Item::nameProperty)
                column("Icon", Item::iconProperty)
            }
        }
    }
}

class DataGridStyles : Stylesheet() {
    init {
        datagridCell and selected {
            opacity = 0.7
        }
    }
}
